package org.zjt.review

import java.io.File

/**
  * DESC    
  *
  * @author
  * @create 2017-06-20 下午2:54
  **/
object Method extends App {


  //匿名函数
  val result = new Function2[String, String, String] {
    override def apply(v1: String, v2: String): String = v1 + " " + v2
  }
  println(result("zhang", "juntao"))


  //匿名函数
  val fun2 = (a: String, b: String) => a + " " + b
  println(fun2("zhang", "juntao"))


  //匿名函数
  def funa = (a: String, b: String) => a + " " + b

  println(funa("zhang", "juntao"))


  //函数
  def fun3(a: String, b: String): String = {
    a + " " + b
  }

  println(fun3("zhang", "juntao"))


  //函数
  def fun4(a: String, b: String) {
    println(a + " " + b)
  }

  fun4("zhang", "juntao")


  def time() = {
    System.currentTimeMillis()
  }


  //可变参数函数
  def catStr(array: String*): String = {
    var var1: String = "";
    for (a <- array) {
      var1 += a
    }
    var1
  }

  println(catStr("zhang", "juntao"))


  //参数默认函数
  def fun5(a: String, b: String = "222"): String = {
    a + " " + b
  }

  println(fun5("zhang"))


  //嵌套函数
  def fun6(a: String, b: String): String = {
    def fun7(a: String, b: String): String = a.toUpperCase() + " " + b.toUpperCase()

    fun7(a, b)
  }

  println(fun6("zhang", "juntao"))


  //偏函数，设置一个变量
  def fun8 = fun5("zhang", _: String)

  println(fun8("san"))


  //指定函数参数名
  println(fun6(a = "zhang", b = "juntao"))


  //递归函数
  def fun9(path: String): Unit = {
    val file = new File(path)
    if (file.isFile()) {
      println("file:" + file.getName)
    } else if (file.isDirectory) {
      file.listFiles().foreach(a => fun9(a.getPath))
    }
  }

  fun9("/Users/zhangjuntao/IdeaProjects/myproject/hw-bigdata/scala-demo")


  //高阶函数：传递函数
  def fun10(f: String => String, b: String): String = f(b)

  println(fun10(_.toUpperCase, "zhang"))


  //柯丽化函数   等价于：def strcat(a: String) = (b: String) => a.toUpperCase() + b.toUpperCase()
  def strcat(a: String)(b: String) = {
    a.toUpperCase() + b.toUpperCase()
  }

  println(strcat("juntao")("zhang"))


  //闭包
  val multiplier = (i: Int) => (i * 2)
  println(multiplier(9))


}
